﻿Public Class frmOrdenCompraListado

    Private Sub frmComprasListado_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        fechaInicio.Value = DateTime.Now.AddDays(-30)
        fechaFin.Value = DateTime.Now
        dgCompras.AutoGenerateColumns = False
        CargarCompras()

    End Sub

    Dim controller As New ControllerSigeco.controller_compras()

    Private _codProveedor As Integer = 0

    Public Property codProveedor() As Integer
        Get
            Return _codProveedor
        End Get
        Set(ByVal value As Integer)
            _codProveedor = value
        End Set
    End Property

    Private m_frmCompras As frmOrdenCompra
    Public Property frmCompras() As frmOrdenCompra
        Get
            Return m_frmCompras
        End Get
        Set(ByVal value As frmOrdenCompra)
            m_frmCompras = value
        End Set
    End Property

    Private Sub CargarCompras()

        Dim dt As New DataTable
        dt = controller.get_Orden_Compras_All(oiml.dbConn, 0, fechaInicio.Value, fechaFin.Value, cboTipo.SelectedValue, _
                                    txtProveedor.Text, txtNroComprobante.Text, "", codProveedor, False, cbxPendientes.Checked)
        dgCompras.DataSource = dt

    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub btnEditar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditar.Click
        Try
            If dgCompras.SelectedRows.Count > 0 Then
                Dim cod As New Integer
                cod = dgCompras.SelectedRows(0).Cells(0).Value

                If frmCompras Is Nothing Then

                    frmCompras = New frmOrdenCompra
                    frmCompras.codOrdenCompra = cod

                    'If frmClientes.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    frmCompras.ShowDialog()
                    CargarCompras()
                    frmCompras.Dispose()
                    frmCompras = Nothing
                    'End If
                Else
                    frmCompras.codOrdenCompra = cod
                    frmCompras.ShowDialog()
                End If

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub txtBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBuscar.Click
        CargarCompras()
    End Sub

    Private Sub txtProveedor_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtNroComprobante.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarCompras()

        End If
    End Sub

    Private Sub dgCompras_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgCompras.DoubleClick
        btnEditar_Click(sender, e)
    End Sub

    Private Sub dgCompras_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgCompras.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnEditar_Click(sender, e)
        End If

    End Sub

    Private Sub btnNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevo.Click
        frmCompras = New frmOrdenCompra()
        frmCompras.codOrdenCompra = 0
        frmCompras.ShowDialog()
        CargarCompras()
    End Sub

    Private m_frmBusProveedor As frmProveedorBuscador
    Public Property frmBusProveedor() As frmProveedorBuscador
        Get
            Return m_frmBusProveedor
        End Get
        Set(ByVal value As frmProveedorBuscador)
            m_frmBusProveedor = value
        End Set
    End Property

    Private Sub btnBuscProveedor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscProveedor.Click
        frmBusProveedor = New frmProveedorBuscador
        frmBusProveedor.ShowDialog()
        txtProveedor.Text = frmBusProveedor.ProveedorDesc
        codProveedor = frmBusProveedor.codProveedor
        If codProveedor > 0 Then
            CargarCompras()
        End If
    End Sub

    Private Sub btnCancelProveedor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelProveedor.Click
        codProveedor = 0
        txtProveedor.Text = ""
        CargarCompras()
    End Sub

    Private Sub cbxPendientes_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxPendientes.CheckedChanged
        CargarCompras()


    End Sub

End Class